package com.lsm.czxxgl_system.controller;

import com.lsm.czxxgl_system.model.domain.Hljbxxb;
import com.lsm.czxxgl_system.service.HljbxxbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: HlglController
 * Package: com.lsm.czxxgl_system.controller
 * Description:
 *
 * @Author lsm
 * @Create 2024/6/27 20:06
 * @Version 1.0
 */
@Controller
@RequestMapping("/hlgl")
public class HlglController {
    @Autowired
    HljbxxbService hljbxxbService;

    @GetMapping("/list")
    public String list(Model model) {
        //查询河流信息列表
        List<Hljbxxb> list = hljbxxbService.list();
        //将河流列表加入请求域
        model.addAttribute("rivers", list);
        return "table/river_table";
    }

    @GetMapping("/getAdd")
    public String getAdd() {
        return "add/add_river";
    }

    @GetMapping("/get/{riverCode}")
    public String getById(@PathVariable String riverCode, Model model) {
        //判断主键是否为空
        if (StringUtils.isBlank(riverCode)) {
            return null;
        }
        //根据主键获取河流信息
        Hljbxxb river = this.hljbxxbService.getByRiverCode(riverCode);
        //保存到请求域
        model.addAttribute("river", river);
        return "update/update_river";
    }

    @PostMapping("/add")
    public String addRiver(Hljbxxb hljbxxb) {
        if (hljbxxb == null) {
            return null;
        }
        if (!hljbxxbService.save(hljbxxb)) {
            return null;
        }
        return "redirect:/hlgl/list";
    }

    /**
     * 根据主键删除河流信息
     *
     * @param riverCode
     * @return
     */
    @DeleteMapping("/delete/{riverCode}")
    public String deleteById(@PathVariable String riverCode) {
        //判断主键是否为空
        if (StringUtils.isBlank(riverCode)) {
            return null;
        }
        if (!this.hljbxxbService.removeByRiverCode(riverCode)) {
            return null;
        }
        return "redirect:/hlgl/list";
    }

    /**
     * 根据主键修改河流信息
     *
     * @param hljbxxb
     * @param riverCodeOri 原始主键
     * @return
     */
    @PutMapping("/update/{riverCodeOri}")
    public String updateById(Hljbxxb hljbxxb, @PathVariable String riverCodeOri) {
        //判断主键和参数是否为空
        if (StringUtils.isBlank(riverCodeOri) || hljbxxb == null) {
            return null;
        }
        if (!hljbxxbService.updateRiver(hljbxxb, riverCodeOri)) {
            return null;
        }
        return "redirect:/hlgl/list";
    }
}
